const db = require("../../lib/conn_oracle")

exports.getMcsListInfo = async (req,res)=>{
    // console.log(req.body)
    let {form} = req.body
    let {lines,stations,begindate,enddate,isxf,desc} = form
    let msg = ""
    let resDatas = {}
    let resStatus = 1
    let searchDatasArr = []
    let lineWhere 
    let staWhere
    let isxfWhere
    let descLike
    let descWhere

    searchDatasArr.push(begindate)
    searchDatasArr.push(enddate)

    if(lines && lines.length === 1){
        lineWhere = `and f.line = :lines`
        searchDatasArr.push(...lines)
    }else if(lines && lines.length > 1){
        lineWhere = `and f.line in (:lines`
        for (let i = 0; i < lines.length; i++)
        lineWhere += (i > 0) ? ", :" + i : ":" + i
        lineWhere += ")"
        searchDatasArr.push(...lines)
    }else{
        lineWhere = ""
    }

    if(stations && stations.length === 1){
        staWhere = `and f.location = :stations`
        searchDatasArr.push(...stations)
    }else if(stations && stations.length > 1){
        staWhere = `and f.location in (:stations`
        for (let i = 0; i < stations.length; i++)
        staWhere += (i > 0) ? ", :" + i : ":" + i
        staWhere += ")"
        searchDatasArr.push(...stations)
    }else{
        staWhere = ""
    }

    isxfWhere = isxf === "true" ? `and f.deviceid is not null` : ""

    if(desc.length){
        descLike = `%${desc}%`
        descWhere = `and f.represent like :descLike`
        searchDatasArr.push(descLike)
    }else{
        descWhere = ""
    }

    let searchSql = `
                     select *
                     from c##wxw.fasevent f
                     where f.eventtime >= to_timestamp(:begindate,'yyyy-mm-dd hh24:mi:ss.ff')
                       and f.eventtime <= trunc(to_timestamp(:enddate,'yyyy-mm-dd hh24:mi:ss.ff'))+1-1/86400
                     ${lineWhere}
                     ${staWhere}
                     ${isxfWhere}
                     ${descWhere}
                     order by f.deviceid,f.code,f.eventtime      
                    `
    // console.log(searchDatasArr)
    // console.log(searchSql)
    try{
        let mcsListInfo = await db.search(searchSql,searchDatasArr)
        resDatas = {mcsListInfo}
        msg = resDatas.mcsListInfo.errorNum ? "查询失败" : "查询成功"
        resStatus = resDatas.mcsListInfo.errorNum ? 0 : 1
        // console.log(mcsListInfo)
        res.json({
            status:resStatus,
            msg,
            resDatas
        })
    }catch(err){
        res.json({
            status:0,
            msg:"查询失败",
            resData:{err}
        })
    }
}